%{
AUTHOR: Felipe Arteaga
-------------------------------------------------------------------------
PROJECT: Warnings
-------------------------------------------------------------------------
DESCRIPTION:
=========================================================================
%}


clearvars -except projectDir projectDirData fromMainWarningsPaper
clc;close all;fclose('all');feature('DefaultCharacterSet','UTF-8');

if(not(exist('projectDir','var')==1&&exist('projectDirData','var')==1&&exist('fromMainWarningsPaper','var')==1&&fromMainWarningsPaper))
    pcName=char(java.lang.System.getProperty('user.name'));
    if(strcmp(pcName,'felipe'))
        % PC Felipe
        myDir='/Users/felipe/Dropbox/';
        projectDir=[myDir,'git/warnings/'];
        projectDirData=[myDir,'projects/warnings/'];
        addpath(genpath([myDir,'/myMatlabFunctions/']));
    end
end
compileLatexTable=false;


dirPlots=[projectDir,'/paper/figuresCL/RDs/'];
dirTable=[projectDir,'/paper/tablesCL/'];

saveTableAndHistogram=true;
tablaDetalles=false;
tablaOutcomes=true;

anhos=2018:2019;
cantAnhos=length(anhos);

entryGrade=[-1 0 1 7 9];

%%
cities=[4001,6001,8001,5001,10001,12001,13001];

cantCities=length(cities);
cityOder=1:cantCities;
nomCities=readtable(sprintf('%s/data/chile/auxiliar/nombresNewMarket2019.csv',projectDirData),'delimiter',',');


tablaCities=table;
tablaCities.newMarket=cities';
tablaCities.cityOder=cityOder';
tablaCities=outerjoin(tablaCities,nomCities,'keys',{'newMarket'},'mergeKeys',true,'type','left');
tablaCities=sortrows(tablaCities,'cityOder');



%%
if(tablaDetalles)
    cantVars=7;
    tabla=nan(cantVars,cantAnhos,cantCities);
    
    
    rowsLab={'Seats','Applicants','% not placed in preference','Unplaced applicants*','Seats after placement'}';
    note='*Does not count applicants not placed in any preference that come from a school that offers the next grade. Those applicants keep their seats in the school of origin.';
    
    
    
    
    
    for i=1:cantAnhos
        % Load apps
        a=anhos(i);
        data=readtable(sprintf('%s/Mineduc/dataCompilada/1. SAE/_csv/compiladoPostulantes%i.csv',myDir,a));
        dataOferta=readtable(sprintf('%s/Mineduc/dataCompilada/1. SAE/_csv/oferta_1_%i.csv',myDir,a));
        
        data.count=ones(height(data),1);
        aux=stataCollapse({'rbdAsign_1','codCursoAsign_1'},data(not(isnan(data.rbdAsign_1)),:),{'count'},'sum','sort',true);
        aux.rbd=aux.rbdAsign_1;
        aux.cod_curso=aux.codCursoAsign_1;
        dataOferta=outerjoin(dataOferta,aux,'keys',{'rbd','cod_curso'},'mergeKeys',true,'type','left');
        dataOferta.count(isnan(dataOferta.count))=0;
        dataOferta.vacantes(dataOferta.vacantes<dataOferta.count)=dataOferta.count(dataOferta.vacantes<dataOferta.count);
        
        dataOferta.seatsLibres=dataOferta.vacantes-dataOferta.count;
        assert(all(dataOferta.seatsLibres>=0))
        
        
        dataOferta=dataOferta(ismember(dataOferta.cod_nivel,entryGrade)&ismember(dataOferta.newMarket,cities),:);
        data=data(ismember(data.grado_1,entryGrade)&data.participa_1==1&ismember(data.newMarket_1,cities),:);
        
        aux=stataCollapse({'newMarket_1'},data,{'asignadoEnPref_1','participa_1','asignadoEnPref_1','sinAsign_1'},{'sum','sum','mean','sum'},'sort',true );
        auxOferta=stataCollapse({'newMarket'},dataOferta,{'vacantes','seatsLibres'},'sum','sort',true );
        
        aux.newMarket=aux.newMarket_1;
        auxF=outerjoin(aux,auxOferta,'keys',{'newMarket'},'mergeKeys',true,'type','left');
        auxF=outerjoin(tablaCities,auxF,'keys',{'newMarket'},'mergeKeys',true,'type','left');
        auxF=sortrows(auxF,'cityOder');
        
        tabla(:,i,:)=[auxF.newMarket,a*ones(height(auxF),1),auxF.vacantes,auxF.participa_1_sum,auxF.asignadoEnPref_1_mean,auxF.sinAsign_1_sum,auxF.seatsLibres]';
        
        
        % Postulantes entry grade, % sin asignacion en pref
        
        % Vacantes
        % Numero de sin colegio
        % Numero de seats after assignment
        
        % Cupos disponibles
        % Cupos usados
        % Cupos libres
    end
    
    %%
    new=reshape(tabla,cantVars,cantAnhos*cantCities);
    new=new(:,not(isnan(tabla(end,:))));
    
    cellImprimir=mat2cellstr(new(3:end,:),'revisarFilas',true);
    
    [esta,pos]=ismember(new(1,:),tablaCities.newMarket);
    
    opts=struct;
    opts.title='';
    %opts.label='';
    opts.header=[tablaCities.nomNewMarket(pos)';mat2cellstr(new(2,:),'wts',0)];
    opts.primeraColumna=rowsLab;
    opts.footnote=note;
    opts.positionParameter='H';
    % opts.panel={0,''};
    %
    opts.withAdjust=true;
    % opts.addColumnNumber=true;
    opts.columnasFantasma=[4 7 9];
    % opts.filasFantasma=[];
    % opts.alignmentFirstCol={'L{3cm}'};
    %
    % opts.standardErrors={};
    % opts.stars=getStars(estimate,se);
    
    %opts.file='';
    
    tabl=cell2latex(cellImprimir,'opts',opts);
    if(compileLatexTable)
    compileLatex(tabl);
    end
end

%% Tabla de RD por año/city


if(tablaOutcomes)
    
    for o=1:2
        switch o 
            case 1
                outcome='addAnyIniEnd';
                titleTable='RD Estimates of Platform Pop-Up  Effects on Adding Any School, by City and Year';
                titleHist='Distribtuion of Estimates for Add Any School,  by City and Year';
                footnote='RD estimates of smart platform pop-up  effects on adding at least one school to the choice application, split by city and year. Cities are sorted by count of 2020 applicants. Santiago is not displayed for 2018 because centralized admission had not yet been rolled out. Estimates from local linear specifications, computed using triangular kernel with bandwidth 0.1. Heteroskedasticity-robust nearest neighbor variance estimator with minimum of 3 neighbors reported in parentheses; computed as in \citet{calonico2014robust}. See section \ref{sec:replication} for details. ';


            case 2
                outcome='schoolsAddedIniEnd';
                titleTable='RD Estimates of Platform Pop-Up  Effects on Schools Added, by City and Year';
                titleHist='Distribtuion of estimates for number of schools added by city and year';
                footnote='RD estimates of smart platform pop-up  effects on schools added to the choice application, split by city and year. Cities sorted by count of 2020 applicants. Santiago not displayed for 2018 because centralized admission had not yet been rolled out. Estimates from local linear specifications, computed using triangular kernel with bandwidth 0.1. Heteroskedasticity-robust nearest neighbor variance estimator with minimum of 3 neighbors reported in parentheses; computed as in \citet{calonico2014robust}. See section \ref{sec:replication} for details. ';

        end
    %%
    cities=[4001,6001,8001,5001,10001,13001];
    
    % Top 10:
    cities=[13001        5001        8001        4001        6001        2001        1001        9001       10001        7001];
    
    %Top 20:
    cities=[ 13001        5001        8001        4001        6001        2001        1001        9001       10001        7001       15001 7002        8002        5003        8003        2002        3001       10002       14001        5002];
    

    cantCities=length(cities);
    cityOder=1:cantCities;
    nomCities=readtable(sprintf('%s/data/chile/auxiliar/nombresNewMarket2019.csv',projectDirData),'delimiter',',');

    nomCities.nomNewMarket(nomCities.newMarket==13001)={'Santiago'};
    nomCities.nomNewMarket(nomCities.newMarket==5001)={'Viña - Valparaíso'};
    nomCities.nomNewMarket(nomCities.newMarket==8001)={'Concepción - Talcahuano'};
    nomCities.nomNewMarket(nomCities.newMarket==6001)={'Rancagua'};
    tablaCities=table;
    tablaCities.newMarket=cities';
    tablaCities.cityOder=cityOder';
    tablaCities=outerjoin(tablaCities,nomCities,'keys',{'newMarket'},'mergeKeys',true,'type','left');
    tablaCities=sortrows(tablaCities,'cityOder');
    
    
    dirData=[projectDirData,'/data/chile/'];
    
    anhos=2018:2020;
    cantAnhos=length(anhos);
    
    matrix_b=nan(cantCities+1,cantAnhos);
    matrix_sd=nan(cantCities+1,cantAnhos);
    
    cantApplicants=nan(cantCities+1,1);
    cutoffs=nan(cantCities+1,1);
    

    

 
    for i=1:cantAnhos
        a=anhos(i);
        load(sprintf([dirData,'%i/inputRD'],a),'dataRD');
        dataRD.riskPopup(dataRD.riskPopup==.3)=.29999999;
        if(a==2017)
            dataRD.riskPopup(dataRD.riskPopup==.5)=.49999999;
            dataRD.riskPopup(dataRD.riskPopup==.7)=.69999999;
        end
        
        restrAll=dataRD.pobPopup==1&dataRD.riskPopup>.01&dataRD.riskPopup<.99;% dataRD.mandatoryToAdd==0;%&ismember(dataRD.grade,entryGrade);
        
        for c=1:cantCities
            
            nm=cities(c);
            
            
            % Avoid mass points at extremes:
            restr=restrAll&dataRD.newMarket==nm;
            
            if(sum(restr)>30)
                if(a==2017)
                    cutoff=mode(dataRD.cutoff(restr));
                    cutoffs(c)=mode(cutoff);
                else
                    cutoff=.3;
                end
                %figure
                res=rdrobust(outcome,'riskPopup',dataRD,'subpop',restr,'cutoff',cutoff,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
                %title(sprintf('nm: %i , year: %i',nm,a))
                % Beta
                matrix_b(c,i)=res.tau_cl;
                % Sd Beta
                matrix_sd(c,i)=res.se_tau_cl;
            end
            
        end
        
        
        %% agregate
        
        
        %figure
        if(a==2017)
            dataRD.riskPopupCentered=dataRD.riskPopup-dataRD.cutoff;
            res=rdrobust(outcome,'riskPopupCentered',dataRD,'subpop',restrAll,'cutoff',0,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        else
            res=rdrobust(outcome,'riskPopup',dataRD,'subpop',restrAll,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        end
        %title(sprintf('nm: %i , year: %i',nm,a))
        % Beta
        matrix_b(end,i)=res.tau_cl;
        % Sd Beta
        matrix_sd(end,i)=res.se_tau_cl;
        
    end
    
    % Add number of applicants 2020:
    for c=1:cantCities
        
        load(sprintf([dirData,'%i/inputRD'],2020),'dataRD');
        nm=cities(c);
        
        % Avoid mass points at extremes:
        cantApplicants(c)=sum(dataRD.newMarket==nm);
        
        
    end
    cantApplicants(end)=height(dataRD);
    
    
    %%
     if(ismember(2017,anhos))
    cellImprimir=mat2cellstr([cantApplicants,cutoffs,matrix_b],'revisarColumnas',true);
     else
         cellImprimir=mat2cellstr([cantApplicants,matrix_b],'revisarColumnas',true);
     end
    
    opts=struct;
    opts.title=titleTable;
    %opts.label='';
    if(ismember(2017,anhos))
    
    opts.header=[{'City','2020 applicants','2017 cutoff'},mat2cellstr(anhos,'wts',0)];
    else
        opts.header=[{'City','2020 applicants'},mat2cellstr(anhos,'wts',0)];
    end
    opts.primeraColumna=[tablaCities.nomNewMarket;{'Chile'}];
    opts.footnote=footnote;
    % opts.panel={0,''};
    opts.label='tab:cities';
    opts.withAdjust=true;
    % opts.addColumnNumber=true;
    opts.filasFantasma=[cantCities];
    opts.positionParameter='H';
    % opts.filasFantasma=[];
    % opts.alignmentFirstCol={'L{3cm}'};
    %
    opts.verticalAdjustParam=7;
    if(ismember(2017,anhos))
    opts.standardErrors=mat2cellstr([nan(size(matrix_sd,1),2),matrix_sd],'wp',true);
    else
        opts.standardErrors=mat2cellstr([nan(size(matrix_sd,1),1),matrix_sd],'wp',true);
    end
    % opts.stars=getStars(estimate,se);
    
    if(saveTableAndHistogram)
    opts.file=[dirTable,outcome,'_byCity'];
    end
    
    tabl=cell2latex(cellImprimir,'opts',opts);
    if(compileLatexTable)
    compileLatex(tabl);
    end
    
    % Histogram of effects
    
    histogram(matrix_b,10)
    %title(titleHist)
    if(saveTableAndHistogram)
    easyExport([dirPlots,'histOfCityYearEstimates_',outcome])
    end
    end
end

